package com.quanqiutong.sunmi_printer;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.quanqiutong.sunmi_printer.utils.SunmiPrintHelper;

/** SunmiPrinterPlugin */
public class SunmiPrinterPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "sunmi_printer");
    channel.setMethodCallHandler(this);
    SunmiPrintHelper.getInstance().initSunmiPrinterService(flutterPluginBinding.getApplicationContext());
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    Map arg = call.arguments();
    System.out.println(call.method);
    System.out.println(arg);
    if (call.method.equals("printerSelfChecking")) {
      SunmiPrintHelper.getInstance().printerSelfChecking();
      result.success(null);
    } 
    else if(call.method.equals("sendRawData")) {
      byte[] data = (byte[]) arg.get("data");
      SunmiPrintHelper.getInstance().sendRawData(data);
      result.success(null);
    }
    else if (call.method.equals("cutPaper")) {
      SunmiPrintHelper.getInstance().cutPaper();
      result.success(null);
    } 
    else if (call.method.equals("cutPaper")) {
      SunmiPrintHelper.getInstance().cutPaper();
      result.success(null);
    } 
    else if (call.method.equals("printerInit")) {
      SunmiPrintHelper.getInstance().printerInit();
      result.success(null);
    } 
    else if (call.method.equals("lineWrap")) {
      SunmiPrintHelper.getInstance().lineWrap((int)arg.get("n"));
      result.success(null);
    } 
    else if (call.method.equals("getPrinterSerialNo")) {
      String res = SunmiPrintHelper.getInstance().getPrinterSerialNo();
      result.success(res);
    } 
    else if (call.method.equals("getPrinterModal")) {
      String res = SunmiPrintHelper.getInstance().getPrinterModal();
      result.success(res);
    } 
    else if (call.method.equals("getPrinterVersion")) {
      String res = SunmiPrintHelper.getInstance().getPrinterVersion();
      result.success(res);
    } 
    else if (call.method.equals("getPrinterPaper")) {
      String res = SunmiPrintHelper.getInstance().getPrinterPaper();
      result.success(res);
    } 
    else if (call.method.equals("setAlignment")) {
      SunmiPrintHelper.getInstance().setAlignment((int)arg.get("align"));
      result.success(null);
    } 
    else if (call.method.equals("setFontSize")) {
      SunmiPrintHelper.getInstance().setFontSize(((Double)arg.get("fontsize")).floatValue());
      result.success(null);
    } 
    else if (call.method.equals("setPrinterStyle")) {
      SunmiPrintHelper.getInstance().setPrinterStyle((int)arg.get("key"),(int)arg.get("value"));
      result.success(null);
    } 
    else if (call.method.equals("printText")) {
      SunmiPrintHelper.getInstance().printText((String)arg.get("text"));
      result.success(null);
    } 
    else if (call.method.equals("printTextWithFont")) {
      SunmiPrintHelper.getInstance().printTextWithFont((String)arg.get("text"),(String)arg.get("typeface"),((Double)arg.get("fontsize")).floatValue());
      result.success(null);
    } 
    else if (call.method.equals("printOriginalText")) {
      SunmiPrintHelper.getInstance().printOriginalText((String)arg.get("text"));
      result.success(null);
    } 
    else if (call.method.equals("printColumnsText")) {
      SunmiPrintHelper.getInstance().printColumnsText((String[])arg.get("colsTextArr"),(int[])arg.get("colsWidthArr"),(int[])arg.get("colsAlign"));
      result.success(null);
    } 
    else if (call.method.equals("printColumnsString")) {
      SunmiPrintHelper.getInstance().printColumnsString((String[])arg.get("colsTextArr"),(int[])arg.get("colsWidthArr"),(int[])arg.get("colsAlign"));
      result.success(null);
    } 
    else if (call.method.equals("printBarCode")) {
      SunmiPrintHelper.getInstance().printBarCode((String)arg.get("data"),(int)arg.get("symbology"),
        (int)arg.get("height"),(int)arg.get("width"),(int)arg.get("textposition"));
      result.success(null);
    } 
    else if (call.method.equals("printQRCode")) {
      SunmiPrintHelper.getInstance().printQRCode((String)arg.get("data"),(int)arg.get("modulesize"),
        (int)arg.get("errorlevel"));
      result.success(null);
    } 
    else if (call.method.equals("printQRCodeImage")) {
      SunmiPrintHelper.getInstance().printQRCodeImage((String)arg.get("data"),(int)arg.get("format"),
        (int)arg.get("width"),(int)arg.get("height"));
      result.success(null);
    } 
    else if (call.method.equals("printBitmap")) {
      SunmiPrintHelper.getInstance().printBitmap((byte[])arg.get("bytes"));
      result.success(null);
    } 
    else if (call.method.equals("updatePrinterState")) {
      int res = SunmiPrintHelper.getInstance().updatePrinterState();
      result.success(res);
    } 
    else if (call.method.equals("labelLocate")) {
      SunmiPrintHelper.getInstance().labelLocate();
      result.success(null);
    } 
    else if (call.method.equals("labelOutput")) {
      SunmiPrintHelper.getInstance().labelOutput();
      result.success(null);
    } 
    else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
